package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        //细节:页面收集的数据可能不全,此时需要额外填充
        appointment.setCreateTime(new Date());

        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        //注意问题: 额外小心3个字段: 创建时间, 状态, 到店时间
        //考虑业务:
        //满足什么条件允许修改
        Appointment obj = appointmentMapper.selectByPrimaryKey(appointment.getId());
        if(obj == null || !Appointment.STATUS_APPOINTMENT.equals(obj.getStatus())){
            throw new BusinessException("预约单只有在预约中状态才可以修改");
        }
        //修改之后会影响哪些数据
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id) {
        //取消条件
        Appointment obj = appointmentMapper.selectByPrimaryKey(id);
        if(obj == null || !Appointment.STATUS_APPOINTMENT.equals(obj.getStatus())){
            throw new BusinessException("预约单只有在预约中状态才可以取消");
        }
        appointmentMapper.changeStatus(id, Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id) {
        //到店条件
        Appointment obj = appointmentMapper.selectByPrimaryKey(id);
        if(obj == null || !Appointment.STATUS_APPOINTMENT.equals(obj.getStatus())){
            throw new BusinessException("预约单只有在预约中状态才可以设置到店状态");
        }
        appointmentMapper.changeStatus(id, Appointment.STATUS_ARRIVAL);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        appointmentMapper.changeStatus(id, status);
    }
}
